/*
 * TowerFactory.java
 *
 * Created on December 9, 2007, 3:48 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.tower;

import hextd.hex.Hex;
import hextd.core.Context;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author hylke
 */
public class TowerFactory {

	public static class TowerCompare implements Comparator<Tower> {

		@Override
		public int compare(Tower o1, Tower o2) {
			int[] l1, l2;
			l1 = o1.getHex().getLocation();
			l2 = o2.getHex().getLocation();
			if (l1[0] != l2[0]) {
				return l1[0] - l2[0];
			}
			if (l1[1] != l2[1]) {
				return l1[1] - l2[1];
			}
			System.err.println("TowerFactory::TowerCompare: Towers are the same!");
			return 0;
		}
	}
	public static TowerCompare towerComparator = new TowerCompare();

	public static enum type {

		firecontrol("fc", TowerFireControl.price),
		upgradeDamage("ud", TowerUpgradeDamage.price),
		upgradeRange("ur", TowerUpgradeRange.price),
		greenOne("g1", TowerGreenOne.price),
		greenTwo("g2", TowerGreenTwo.price),
		greenThree("g3", TowerGreenThree.price),
		redOne("r1", TowerRedOne.price),
		redTwo("r2", TowerRedTwo.price),
		redThree("r3", TowerRedThree.price),
		yellowOne("y1", TowerYellowOne.price),
		yellowTwo("y2", TowerYellowTwo.price),
		yellowThree("y3", TowerYellowThree.price),
		blueOne("b1", TowerBlueOne.price),
		blueTwo("b2", TowerBlueTwo.price),
		blueThree("b3", TowerBlueThree.price),
		homeBase("hb", TowerHome.price),
		homeBaseAI("ai", TowerHomeAI.price);
		/**
		 * price: the base price of a new tower of this type.
		 */
		public final int price;
		/**
		 * code: the two letter code of this tower type.
		 */
		public final String code;

		type(String code, int price) {
			this.price = price;
			this.code = code;
		}
	}
	public static Map<String, type> table = new HashMap<String, type>();

	static {
		for (type t : type.values()) {
			table.put(t.code, t);
		}
	}

	public static Tower createTower(type t, Context c, Hex h, int owner, int activateTick) {
		switch (t) {
			case upgradeDamage:
				return new TowerUpgradeDamage(c, h, owner, activateTick);
			case upgradeRange:
				return new TowerUpgradeRange(c, h, owner, activateTick);
			case firecontrol:
				return new TowerFireControl(c, h, owner, activateTick);
			case greenOne:
				return new TowerGreenOne(c, h, owner, activateTick);
			case greenTwo:
				return new TowerGreenTwo(c, h, owner, activateTick);
			case greenThree:
				return new TowerGreenThree(c, h, owner, activateTick);
			case redOne:
				return new TowerRedOne(c, h, owner, activateTick);
			case redTwo:
				return new TowerRedTwo(c, h, owner, activateTick);
			case redThree:
				return new TowerRedThree(c, h, owner, activateTick);
			case yellowOne:
				return new TowerYellowOne(c, h, owner, activateTick);
			case yellowTwo:
				return new TowerYellowTwo(c, h, owner, activateTick);
			case yellowThree:
				return new TowerYellowThree(c, h, owner, activateTick);
			case blueOne:
				return new TowerBlueOne(c, h, owner, activateTick);
			case blueTwo:
				return new TowerBlueTwo(c, h, owner, activateTick);
			case blueThree:
				return new TowerBlueThree(c, h, owner, activateTick);
			case homeBase:
				return new TowerHome(c, h, owner, activateTick);
			case homeBaseAI:
				return new TowerHomeAI(c, h, owner, activateTick);
		}
		return null;
	}
}
